package controller

import (
	"encoding/json"
	"net/http"
	"sync"

	"github.com/gogf/gf/frame/g"
	"github.com/gorilla/websocket"
)

type User struct {
	Conn    *websocket.Conn
	Name    string
	Id      string
	Avator  string
	To_id   string
	Role_id string
}
type Message struct {
	conn        *websocket.Conn
	context     interface{}
	content     []byte
	messageType int
}
type TypeMessage struct {
	Type interface{} `json:"type"`
	Data interface{} `json:"data"`
}
type ClientMessage struct {
	Name      string `json:"name"`
	Avator    string `json:"avator"`
	Id        string `json:"id"`
	VisitorId string `json:"visitor_id"`
	Group     string `json:"group"`
	Time      string `json:"time"`
	ToId      string `json:"to_id"`
	Content   string `json:"content"`
	City      string `json:"city"`
	ClientIp  string `json:"client_ip"`
	Refer     string `json:"refer"`
	IsKefu    string `json:"is_kefu"`
}

var ClientList = make(map[string]*User)
var KefuList = make(map[string][]*User)
var message = make(chan *Message, 10)
var upgrader = websocket.Upgrader{}
var Mux sync.RWMutex

func init() {
	upgrader = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
		// 解决跨域问题
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
	//go UpdateVisitorStatusCron()
	go WsServerBackend()
}

//后端广播发送消息
func WsServerBackend() {
	for {
		message := <-message
		var typeMsg TypeMessage
		json.Unmarshal(message.content, &typeMsg)
		conn := message.conn
		// if typeMsg.Type == nil || typeMsg.Data == nil {
		// 	continue
		// }
		msgType := typeMsg.Type.(string)
		g.Log().Debug("客户端:", string(message.content))

		switch msgType {
		//心跳
		case "ping":
			msg := TypeMessage{
				Type: "pong",
			}
			str, _ := json.Marshal(msg)
			Mux.Lock()
			conn.WriteMessage(websocket.TextMessage, str)
			Mux.Unlock()
		}

	}
}
func UpdateVisitorUser(visitorId string, toId string) {
	guest, _ := ClientList[visitorId]
	guest.To_id = toId
}
